package com.cooper.moviedb.bo;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: cooperlee
 * Date: 11/19/12
 * Time: 12:29 PM
 */
@javax.persistence.Table(name = "MEDIA", schema = "", catalog = "media_service_db")
@Entity
public class MediaEntity {

    private String detailsPageEncode;

    public String getDetailsPageEncode() {
        return detailsPageEncode;
    }

    public void setDetailsPageEncode(String detailsPageEncode) {
        this.detailsPageEncode = detailsPageEncode;
    }

    private String id;

    @javax.persistence.Column(name = "ID", nullable = false, insertable = true, updatable = true, length = 50, precision = 0)
    @Id
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    private MediaEntity parent;

    @ManyToOne()
    @JoinColumn(name = "PARENT_ID", referencedColumnName = "ID")
    public MediaEntity getParent() {
        return parent;
    }

    public void setParent(MediaEntity parent) {
        this.parent = parent;
    }

    private Set<MediaEntity> childs = new HashSet<MediaEntity>(0);


    @OneToMany(mappedBy = "parent")
    public Set<MediaEntity> getChilds() {
        return childs;
    }

    public void setChilds(Set<MediaEntity> childs) {
        this.childs = childs;
    }

    private int page;

    @javax.persistence.Column(name = "OUT_PAGE")
    @Basic
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    private boolean root;

    @javax.persistence.Column(name = "ROOT", nullable = false, insertable = true, updatable = true, length = 0, precision = 0)
    @Basic
    public boolean isRoot() {
        return root;
    }

    public void setRoot(boolean root) {
        this.root = root;
    }

    private int type;

    @javax.persistence.Column(name = "TYPE", nullable = true, insertable = true, updatable = true, length = 10, precision = 0)
    @Basic
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    private boolean deleted;

    @javax.persistence.Column(name = "DELETED", nullable = true, insertable = true, updatable = true, length = 0, precision = 0)
    @Basic
    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    private Timestamp joinTime;

    @javax.persistence.Column(name = "JOIN_TIME", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public Timestamp getJoinTime() {
        return joinTime;
    }

    public void setJoinTime(Timestamp joinTime) {
        this.joinTime = joinTime;
    }

    private Timestamp checkDate;

    @javax.persistence.Column(name = "CHECK_DATE")
    @Basic
    public Timestamp getCheckDate() {
        return checkDate;
    }

    public void setCheckDate(Timestamp checkDate) {
        this.checkDate = checkDate;
    }

    private Timestamp lastChangeDate;

    @javax.persistence.Column(name = "LAST_CHANGE_DATE")
    @Basic
    public Timestamp getLastChangeDate() {
        return lastChangeDate;
    }

    public void setLastChangeDate(Timestamp lastChangeDate) {
        this.lastChangeDate = lastChangeDate;
    }

    private int source;

    @javax.persistence.Column(name = "SOURCE", nullable = true, insertable = true, updatable = true, length = 10, precision = 0)
    @Basic
    public int getSource() {
        return source;
    }

    public void setSource(int source) {
        this.source = source;
    }

    private Set<MediaInfoEntity> mediaInfoEntitySet = new HashSet<MediaInfoEntity>();

    @OneToMany(mappedBy = "mediaEntity", cascade = CascadeType.ALL)
    public Set<MediaInfoEntity> getMediaInfoEntitySet() {
        return mediaInfoEntitySet;
    }

    public void setMediaInfoEntitySet(Set<MediaInfoEntity> mediaInfoEntitySet) {
        this.mediaInfoEntitySet = mediaInfoEntitySet;
    }

    private Set<MediaUriEntity> mediaUriEntitySet = new HashSet<MediaUriEntity>();

    @OneToMany(mappedBy = "mediaEntity", cascade = CascadeType.ALL)
    public Set<MediaUriEntity> getMediaUriEntitySet() {
        return mediaUriEntitySet;
    }

    public void setMediaUriEntitySet(Set<MediaUriEntity> mediaUriEntitySet) {
        this.mediaUriEntitySet = mediaUriEntitySet;
    }

    @Transient
    public String getDetailsTitle() {
        for (MediaInfoEntity info : getMediaInfoEntitySet()) {
            if (info.getKeyName().equals("title")) {
                return info.getValue();
            }
        }
        return null;
    }

    @Transient
    public boolean existsUrl(String url) {
        for (MediaUriEntity urlEntity: getMediaUriEntitySet()){
            if (urlEntity.getUrl().equals(url)){
                return true;
            }
        }
        return false;
    }

    @Transient
    public String getDetailsUrl() {
        for (MediaInfoEntity info : getMediaInfoEntitySet()) {
            if (info.getKeyName().equals("details_url")) {
                return info.getValue();
            }
        }
        return null;
    }

    @Transient
    public void putSrcUrls() {
        for (MediaInfoEntity info : getMediaInfoEntitySet()) {
            if (info.getKeyName().equals("play")) {
                getMediaUriEntitySet().add(new MediaUriEntity(this, info.getValue(), getJoinTime(), false));
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MediaEntity that = (MediaEntity) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;


        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        return result;
    }
}
